package me.flyray.bsin.blockchain.bsc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.Transaction;
import org.web3j.protocol.http.HttpService;

import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.blockchain.BsinBlockChainEngine;
import me.flyray.bsin.blockchain.enums.ChainType;
import me.flyray.bsin.blockchain.utils.Java2ContractTypeConcert;
import me.flyray.bsin.constants.ResponseCode;
import me.flyray.bsin.exception.BusinessException;

/**
 * @author bolei
 * @date 2023/8/2 19:58
 * @desc
 */

@Slf4j
@Component
public class BscBiz implements BsinBlockChainEngine {

    private String testnetEndpointAddress;
    private String mainnetEndpointAddress;
    private String chainType;
    private Web3j web3;
    private Web3j web3Test;

    public BscBiz() {
        this.testnetEndpointAddress = "https://rpc-mumbai.maticvigil.com";
        this.mainnetEndpointAddress = "https://rpc.ankr.com/polygon";
//        this.web3Test = Web3.create(this.confluxRpcUrlTest, 3, 10000);
//        this.web3 = Web3.create(this.confluxRpcUrlMain, 3, 10000);
        this.web3 = Web3j.build(new HttpService(mainnetEndpointAddress));
        this.web3Test = Web3j.build(new HttpService(testnetEndpointAddress));
        chainType = ChainType.BSC.getCode();
    }

    @Override
    public Map<String, String> createWallet(String password, String chainEnv) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> createAccount(String chainEnv, String privateKey, String gasPrice, String gasLimit, String value) {
        return null;
    }

    @Override
    public Map<String, Object> contractDeploy(String chainEnv, String privateKey, String gasPrice, String gasLimit, String value, String byteCode, List<Java2ContractTypeConcert> functionReturnType, List<Java2ContractTypeConcert> functionInputParams) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> transfer(String chainEnv, String fromPrivateKey, String gasPrice, String gasLimit, String value, String toAddress) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> contractWrite(String chainEnv, String privateKey, String gasPrice, String gasLimit, String value, String contractAddress, String method, List<Java2ContractTypeConcert> functionReturnType, List<Java2ContractTypeConcert> functionInputParams) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> contractWrite(String chainEnv, String privateKey, String gasPrice, String gasLimit, String value, String contractAddress, String method, String methodId, List<Java2ContractTypeConcert> functionReturnType, List<Java2ContractTypeConcert> functionInputParams) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> contractRead(String chainEnv, String contractAddress, String method, List<Java2ContractTypeConcert> functionReturnType, List<Java2ContractTypeConcert> functionInputParams) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> getTransaction(String txHash) throws IOException {
        //通过hash获取交易
        Optional<Transaction> transactions = web3.ethGetTransactionByHash(txHash).send().getTransaction();
        if (!transactions.isPresent()) {
            // 交易不存在
            throw new BusinessException(ResponseCode.HASH_NON_EXISTENT);
        }
        Map result = new HashMap();
        Transaction transactionInfo = transactions.get();
        result.put("transaction",JSONObject.toJSON(transactionInfo));
        System.out.println(JSONObject.toJSON(transactionInfo));
        return result;
    }

    @Override
    public Map<String, Object> getSponsor(Map<String, Object> requestMap) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> setSponsor(Map<String, Object> requestMap) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> addWhiteList(Map<String, Object> requestMap) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> isWhitelisted(Map<String, Object> requestMap) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> removeWhiteList(Map<String, Object> requestMap) throws Exception {
        return null;
    }

    @Override
    public String getAddress(String chainEnv, String privateKey) {
        return null;
    }

    @Override
    public String getPrivateKey() {
        return null;
    }

    @Override
    public String getBalance(String chainEnv, String address) throws Exception {
        return null;
    }

    @Override
    public String getAssetBalance(String chainEnv, String contractAddress, String accountAddress, String assetProtocol, String tokenId) throws Exception{
        return null;
    }

    @Override
    public String getIpfsGateway() {
        return null;
    }

    @Override
    public String getZeroAddress(String chainEnv) {
        return null;
    }

    @Override
    public String getWeb3ClientVersion(String chainEnv) {
        return null;
    }

    @Override
    public long getBlockHeight(String chainEnv) {
        return 0;
    }

    @Override
    public String getTreasuryContractAddress(String chainEnv, String daoAddressStr, String wrapperOrExtensionName, boolean byFactory, boolean isInsert) {
        return null;
    }

    @Override
    public String getVaultsContractAddress(String chainEnv, String daoAddressStr, String wrapperOrExtensionName, boolean byFactory, boolean isInsert) {
        return null;
    }

    @Override
    public String getDaoWrapperOrExtensionAddressByName(String chainEnv, String daoAddressStr, String wrapperOrExtensionName, boolean isInsert) {
        return null;
    }

    @Override
    public String getDaoNameByAddress(String chainEnv, String daoAddressStr, boolean isInsert) {
        return null;
    }

    @Override
    public String getDaoAddressByIndex(String chainEnv, String daoIndex, boolean isInsert) {
        return null;
    }

    @Override
    public String getDaoAddressByName(String chainEnv, String daoName, boolean isInsert) {
        return null;
    }
}
